package linwen.wang.schedule.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TestSchedulExecutor {
    private static int count = 0;
    public static void main(String[] args) {
        //创建 run 方法
        Runnable runnable = () -> {
            count++;
            log.info("current loop:{}",count);
            log.info(Thread.currentThread().getName()+"==start  "+new Date());
            if(count==5){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            log.info(Thread.currentThread().getName()+"==end  "+new Date());
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        // 同 scheduleAtFixedRate
        service.scheduleAtFixedRate(runnable, 0, 2, TimeUnit.SECONDS);
    }
}
